﻿#include <boost/interprocess/allocators/allocator.hpp>
#include <boost/interprocess/containers/map.hpp>
#include <boost/interprocess/containers/string.hpp>
#include <boost/interprocess/containers/vector.hpp>
#include <boost/interprocess/managed_shared_memory.hpp>
#include <boost/interprocess/sync/interprocess_condition.hpp>
#include <boost/interprocess/sync/interprocess_mutex.hpp>
#include <vector>

class custom_data {
public:
    typedef boost::interprocess::allocator<int, boost::interprocess::managed_shared_memory::segment_manager> shmem_vector_allocator;
    typedef boost::interprocess::vector<int, shmem_vector_allocator> my_vector;
    typedef boost::interprocess::allocator<boost::interprocess::string, boost::interprocess::managed_shared_memory::segment_manager> shmem_vector_string_allocator;
    typedef boost::interprocess::vector<boost::interprocess::string, shmem_vector_string_allocator> vector_string;

    typedef int key_type;
    typedef uint64_t mapped_type;
    typedef std::pair<const int, uint64_t> value_type;
    typedef boost::interprocess::allocator<value_type, boost::interprocess::managed_shared_memory::segment_manager> shmem_map_allocator;
    typedef boost::interprocess::map<key_type, mapped_type, std::less<key_type>, shmem_map_allocator> my_map;

    custom_data();
    void init(boost::interprocess::managed_shared_memory &segment);

    void add();

    void push_back(int num);
    void push_back(std::string str);

    // std::vector<int> pop_all(std::size_t &index);
    std::vector<std::string> pop_all();

    void print();

private:
    int *a_ptr_ = nullptr;
    boost::interprocess::interprocess_mutex *a_mutex_ptr_ = nullptr;

    // my_vector *b_ptr_ = nullptr;
    my_map *b_ptr_ = nullptr;
    boost::interprocess::interprocess_mutex *b_mutex_ptr_ = nullptr;
    boost::interprocess::interprocess_condition *b_cond_ptr_ = nullptr;

    vector_string *v_s_ptr = nullptr;
};
